<?php
class Cms_View_Helper_SendAjaxForm extends Zend_View_Helper_Abstract
{
    public $view;
 
    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }
 
    /**
     * Validate dojo enabled form onSubmit.
     *
     * @param  string $formId
     * @return void
     */
    public function SendAjaxForm($formId, $url)
    {
        $this->view->headScript()->captureStart(); ?>
        function validateForm() {
            var form = dijit.byId("<?php echo $formId; ?>");
            if (!form.validate()) {
                return false;
            }
           
	        dojo.xhrPost ({
				// The page that parses the POST request
				url: '<?php echo $url;?>',
			   	
                // Name of the Form we want to submit
				form: '<?php echo $formId; ?>',
				handleAs: 'json',
                
				// Loads this function if everything went ok
                load: function (data) {
                	if(typeof sendAjaxData == 'function') {
						sendAjaxData(data);
					} 
				}	
			});
        }
        
        function initForm() {
        	dojo.connect(dijit.byId('submit'), "onClick", "validateForm");
        }
        <?php $this->view->headScript()->captureEnd();
    }
}